import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}

version := "0.1"
val nameStr = "live_forest_scala_js"
val scalaVersionStr = "2.13.1"

lazy val server = (project in file("server"))
  .settings(commonSettings)
  .settings(
    scalaJSProjects := Seq(client),
    name := nameStr,
    version := "1.0",
    pipelineStages in Assets := Seq(scalaJSPipeline),
    pipelineStages := Seq(digest, gzip),
    compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline).value,
    libraryDependencies ++= Seq(
      "com.vmunier" %% "scalajs-scripts" % "1.1.4",
      guice,
      specs2 % Test,
      ehcache,
      "com.typesafe.play" %% "play-slick" % "4.0.2",
      "com.typesafe.slick" %% "slick-codegen" % "3.3.2",
      "mysql" % "mysql-connector-java" % "5.1.25",
      "com.github.tototoshi" %% "slick-joda-mapper" % "2.4.1",
      "commons-io" % "commons-io" % "2.5",
      "org.apache.poi" % "poi-ooxml" % "3.15",
      "org.apache.commons" % "commons-math3" % "3.6.1",
      "com.aliyun" % "aliyun-java-sdk-core" % "3.7.1",
      "com.aliyun" % "aliyun-java-sdk-dysmsapi" % "1.1.0",
      "com.typesafe.play" %% "play-joda-forms" % "2.7.3",
      "org.apache.xmlgraphics" % "batik-codec" % "1.10",
      "org.jxls" % "jxls" % "2.4.7",
      "com.itextpdf" % "itext7-core" % "7.1.4",
      "com.hhandoko" %% "play28-scala-pdf" % "4.3.0",
      "net.logstash.logback" % "logstash-logback-encoder" % "5.3",
      "org.scala-lang.modules" %% "scala-parallel-collections" % "0.2.0",
      "org.apache.commons" % "commons-lang3" % "3.6",
      "javax.mail" % "mail" % "1.4.7",
      "com.lihaoyi" %% "upickle" % "2.0.0",
      "org.typelevel" %% "cats-core" % "2.0.0"
    ),
    scalacOptions ++= {
      Seq(
        "-Ymacro-annotations"
      )
    }
  )
  .enablePlugins(PlayScala, WebScalaJSBundlerPlugin)
  .dependsOn(sharedJvm)

val reactVersion = "17.0.2"

lazy val client = (project in file("client"))
  .settings(commonSettings)
  .settings(
    scalaJSUseMainModuleInitializer := false,
    useYarn := true,
    webpack / version := "4.40.2",
    resolvers += "jitpack" at "https://jitpack.io",
    libraryDependencies ++= Seq(
      "com.lihaoyi" %%% "scalatags" % "0.11.1",
      "me.shadaj" %%% "slinky-core" % "0.7.2",
      "me.shadaj" %%% "slinky-web" % "0.7.2",
      "org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0",
      "com.lihaoyi" %%% "upickle" % "2.0.0",
      "com.softwaremill.quicklens" %%% "quicklens" % "1.6.1"
    ),
    Compile / npmDependencies ++= Seq(
      "react" -> reactVersion,
      "@types/react" -> reactVersion,
      "d3" -> "4.13.0",
      "webpack-merge" -> "4.2.2",
      "jquery" -> "3.5.0",
      "react-hook-form" -> "7.37.0",
      "react-dom" -> reactVersion,
      "react-select" -> "5.4.0",
      "bootstrap-sweetalert" -> "1.0.1"
    ),
    stFlavour := Flavour.Slinky,
    stIgnore := List(
      "d3",
      "webpack-merge",
      "jquery",
      "react-hook-form",
      "react-dom",
      "react-select",
      "bootstrap-sweetalert"
    ),
    scalacOptions ++= {
      Seq(
        "-Ymacro-annotations"
      )
    },
    webpackBundlingMode := BundlingMode.LibraryAndApplication(),
    webpackConfigFile := Some(
      baseDirectory.value / "my.custom.webpack.config.js"
    )
  )
  .enablePlugins(ScalaJSBundlerPlugin, ScalablyTypedConverterPlugin)
  .dependsOn(sharedJs)

lazy val shared = crossProject(JSPlatform, JVMPlatform)
  .crossType(CrossType.Pure)
  .in(file("shared"))
  .settings(commonSettings)
  .settings(
    scalacOptions ++= {
      Seq("-Ymacro-annotations")
    },
    libraryDependencies ++= Seq(
      "com.lihaoyi" %% "upickle" % "2.0.0"
    )
  )
lazy val sharedJvm = shared.jvm
lazy val sharedJs = shared.js

lazy val commonSettings = Seq(
  scalaVersion := scalaVersionStr,
  resolvers ++= Seq(
    Resolver.sonatypeRepo("snapshots")
  )
)

onLoad in Global := (onLoad in Global).value andThen { s: State =>
  "project server" :: s
}
